package mobi.ilabs.restroom;

import mobi.ilabs.restroom.storage.Storage;
import mobi.ilabs.restroom.storage.StorageFactory;
import mobi.ilabs.restroom.storage.StorageRestlet;

import org.restlet.Context;
import org.restlet.Guard;
import org.restlet.Restlet;
import org.restlet.data.ChallengeScheme;


/**
 * An adapter that adapts to the storage infrastructure by
 * providing a pair of restlets for accessing objects identified
 * by their object IDs or associated filenames.
 *
 */
public class StorageAdapter {

    final Restlet filestore;
    final Restlet obstore;


    // XXX This should be accessed from configuration!
    private final static String AUTHENTICATION_REALM = "restroom";


//  incoming HTTP request to storage transaction.

    // XXX There is some conceptually weird shit going on here!
    //     Storage is being referenced in several ways, and its highly
    //     obscure how these references interact.   That should change, and
    //     it should change in a way that makes the following nine 
    //     lines of code (excluding blank lines) really, really clear, to the point of being trivial!

//  Setting up the processing/filter chain from 

    public StorageAdapter(final Context context) {
        
        assert( context != null);

        final Storage storage = StorageFactory.getStorage();

        final StorageRestlet   storageRestlet = storage.newRestlet(context);


        final Restlet  unGuardedRestroom = new HttpMethodOverriderRestlet(storageRestlet);

        final Guard      guardedRestroom = 
            new RestroomGuard(context, ChallengeScheme.HTTP_BASIC, AUTHENTICATION_REALM);
        guardedRestroom.setNext(unGuardedRestroom);

        final ObStoreRestlet innerObstore = new ObStoreRestlet(context, guardedRestroom);  
        filestore  = storageRestlet.newContext(new FileStore(context, storage, innerObstore));
        obstore  = storageRestlet.newContext(innerObstore);

    }


    public Restlet getFilestore() {
        return filestore;
    }


    public Restlet getObstore() {
        return obstore;
    }
    
    

}
